package com.sixfoot.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.sixfoot.hrv.codec.protobuf.DataEpReqProto;
import com.sixfoot.hrv.codec.protobuf.DataIbiReqProto;
import com.sixfoot.hrv.codec.protobuf.DataPpgReqProto;
import com.sixfoot.hrv.codec.protobuf.RequestProto;
import com.sixfoot.hrv.codec.protobuf.RequestProto.Request;
import com.sixfoot.hrv.codec.protobuf.ResourceProto;
import com.sixfoot.hrv.codec.protobuf.ScaleListReqProto;
import com.sixfoot.hrv.codec.protobuf.ScaleReqProto;
import com.sixfoot.hrv.codec.protobuf.StartScaleReqProto;
import com.sixfoot.hrv.codec.protobuf.StartScaleReqProto.StartScaleReq;
import com.sixfoot.hrv.codec.protobuf.StopScaleReqProto;

import io.netty.channel.Channel;

public class Scale {
	Channel channel;
	Timer ppgTimer;
	Timer epTimer;
	Timer hrvTimer;
	Timer ibiTimer;
	
	List<Float> ppgValues;
	List<Float> spectrumValues;
	
	public Scale(Channel channel) {
		this.channel = channel;
	}
	
	public void getList() {
		ResourceProto.Resource resource = ResourceProto.Resource.newBuilder()
				.setId("12")
				.build();
		
		ScaleListReqProto.ScaleListReq scaleListReq = ScaleListReqProto.ScaleListReq.newBuilder()
			.setResource(resource)
			.setPageNum(0)
			.setPageSize(10)
			.build();
		
		RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
		builder.setType(Request.Type.GET_SCALE_LIST);
		builder.setExtension(ScaleListReqProto.ScaleListReq.scaleListReq, scaleListReq);
		channel.writeAndFlush(builder.build());
	}
	
	public void getScale() {
		// TODO Auto-generated method stub
		ScaleReqProto.ScaleReq scaleReq = ScaleReqProto.ScaleReq.newBuilder()
			.setScaleId("2c9da6835847097e0158471e97cd0002")
			.build();
		
		RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
		builder.setType(Request.Type.GET_SCALE);
		builder.setExtension(ScaleReqProto.ScaleReq.scaleReq, scaleReq);
		channel.writeAndFlush(builder.build());
	}
	
	public void start() {
		ppgTimer = new Timer();
		epTimer = new Timer();
		hrvTimer = new Timer();
		ibiTimer = new Timer();
		
		ppgValues = new ArrayList<Float>();
		ppgValues.add(0.1f);
		ppgValues.add(66.32f);
		ppgValues.add(1.35f);
		ppgValues.add(5.232f);
		ppgValues.add(2.32f);
		ppgValues.add(21.3222f);
		
		spectrumValues = new ArrayList<>();
		spectrumValues.add(0.0f);
		spectrumValues.add(1.0f);
		spectrumValues.add(2.0f);
		spectrumValues.add(3.0f);
		
		// 测试资源-菩提树
		ResourceProto.Resource resource = ResourceProto.Resource.newBuilder()
				.setId("12")
				.build();
		
		// 发送开始记录数据的请求
		StartScaleReqProto.StartScaleReq startScale = StartScaleReqProto.StartScaleReq.newBuilder()
				.setDifficulty(StartScaleReq.Difficulty.EASY)
				.setResource(resource)
				.build();
		
		RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
		builder.setType(Request.Type.START_SCALE);
		builder.setExtension(StartScaleReqProto.StartScaleReq.startScaleReq, startScale);
		channel.writeAndFlush(builder.build());
	}
	
	public void stop() {
		ppgTimer.cancel();
		epTimer.cancel();
		hrvTimer.cancel();
		ibiTimer.cancel();
		
		StopScaleReqProto.StopScaleReq stopScale = StopScaleReqProto.StopScaleReq.newBuilder()
				.setRemark("测试一下")
				.addAllEvaluation(new ArrayList<Float>(){{add(44.2f); add(44.22f);}})
				.addAllFrequencyDomain(new ArrayList<Float>(){{add(111.2f); add(11.2f);}})
				.addAllTimeDomain(new ArrayList<Float>(){{add(22.3f); add(22.4f);}})
				.addAllSpectrum(new ArrayList<Float>(){{add(33.3f); add(33.4f);}})
				.build();
		
		RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
		builder.setType(Request.Type.STOP_SCALE);
		builder.setExtension(StopScaleReqProto.StopScaleReq.stopScaleReq, stopScale);
		channel.writeAndFlush(builder.build());
	}
	
	public void startScale() {
		this.ppg();
		this.ep();
		this.hrv();
		this.ibi();
	}
	
	public void ppg() {
		ppgTimer.schedule(new TimerTask() {
			public void run() {
				DataPpgReqProto.DataPpgReq ppg = DataPpgReqProto.DataPpgReq.newBuilder()
						.addAllValue(ppgValues)
						.build();
				
				RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
				builder.setType(Request.Type.DATA_PPG);
				builder.setExtension(DataPpgReqProto.DataPpgReq.dataPpgReq, ppg);
				channel.writeAndFlush(builder.build());
			}
		}, 0, 55);
	}
	
	public void ep() {
		epTimer.schedule(new TimerTask() {
			public void run() {
				DataEpReqProto.DataEpReq ep = DataEpReqProto.DataEpReq.newBuilder()
						.addValue(50.0f)
						.addAllSpectrum(spectrumValues)
						.build();
				
				RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
				builder.setType(Request.Type.DATA_EP);
				builder.setExtension(DataEpReqProto.DataEpReq.dataEpReq, ep);
				channel.writeAndFlush(builder.build());
			}
		}, 0, 5000);
	}
	
	public void hrv() {
		hrvTimer.schedule(new TimerTask() {
			public void run() {
//				System.out.println("-------设定要指定任务--------");
			}
		}, 0, 500);
	}
	
	public void ibi() {
		ibiTimer.schedule(new TimerTask() {
			public void run() {
				DataIbiReqProto.DataIbiReq ibi = DataIbiReqProto.DataIbiReq.newBuilder()
						.addAllValue(new ArrayList<Float>(){{add(3.0f); add(888.8f);}})
						.build();
				
				RequestProto.Request.Builder builder = RequestProto.Request.newBuilder();
				builder.setType(Request.Type.DATA_IBI);
				builder.setExtension(DataIbiReqProto.DataIbiReq.dataIbiReq, ibi);
				channel.writeAndFlush(builder.build());
			}
		}, 0, 500);
	}

	
}
